﻿using System.Linq;
using System.Web.Mvc;
using System.Collections.Generic;
using System;
using ASPNETMVCApplication.Code.Filters;
using ASPNETMVCApplication.Repositories;
using ASPNETMVCApplication.ActionServiceReference;
using ASPNETMVCApplication.Code.HtmlHelpers;
using ASPNETMVCApplication.Controllers;
using ASPNETWebApplication;
using ASPNETMVCApplication.Areas.Shop.Models;
using ASPNETMVCApplication.Areas.Cust.ViewModels;
using System;
using ASPNETMVCApplication.Areas.Cust.Models;

namespace ASPNETMVCApplication.Areas.Cust.Controllers
{
    public class CustomerController : Controller
    {
       private ICustomerRepository _customerRepository;
       private IOrderRepository _orderRepository;
       ITrafficRepository _trafficRepository;
        ICategoryRepository _categoryRepository;
        ISupplierRepository _supplierRepository;
        
        public CustomerController()
            : this(new CustomerRepository(),new OrderRepository(),new TrafficRepository(),new CategoryRepository(),new SupplierRepository())
        {
        }

        /// <summary>
        /// Overloaded 'injectable' Constructor for ShopController.
        ///
        /// Pattern: Constructor Dependency Injection (DI).
        /// </summary>
        /// <param name="categoryRepository"></param>
        /// <param name="productRepository"></param>
        /// <param name="cartRepository"></param>
        public CustomerController(ICustomerRepository customerRepository,IOrderRepository orderRepository,ITrafficRepository trafficRepository,ICategoryRepository categoryRepository,ISupplierRepository supplierRepository)
        {
            _customerRepository = customerRepository;
            _orderRepository = orderRepository;
            _trafficRepository = trafficRepository;
            _categoryRepository = categoryRepository;
            _supplierRepository = supplierRepository;
        }

        //[Menu(MenuItem.Customer)]
        public ActionResult Index()
        {
            //var listCustomer = Mapper.ToModel(_customerRepository.GetList(null));
            //return View(listCustomer);
            if (Session["custId"] != null)
            {
                OrderViewModels orderViewModels = null;
                Supplier supplier = null;
                Traffic traffic = null;
                Category category = null;
                List<OrderDetail> orderDetail = null;
                List<OrderViewModels> listViewModel = new List<OrderViewModels>();
                int custId=(int)Session["custId"];
                var listOrder =_orderRepository.GetOrderList(custId,0,DateTime.Parse("01/01/1990"),DateTime.Now,0);
                foreach (Order od in listOrder.ToList())
                {
                    supplier = _supplierRepository.Get(od.SupplierId);
                    orderDetail = _orderRepository.Get(od.OrderId).OrderDetails.ToList();
                    category = _categoryRepository.Get(orderDetail[0].CategoryId);
                    traffic = _trafficRepository.Get(category.TrafficId, od.SupplierId);
                    orderViewModels =new OrderViewModels{ _order=od,_listOrderDetail=orderDetail,_category=category,_supplier=supplier,_traffic=traffic };
                    listViewModel.Add(orderViewModels);
                }
                ViewData["listOrderNew"] = listViewModel;
                return View();
            }
            else
            {
                return RedirectToAction("Login");
            }
        }

        //
        public ActionResult Register()
        {

            return View();
        }
        //register account supplier
        [HttpPost]
        public ActionResult Register(CustomerModel custModel)
        {
            if (ModelState.IsValid)
            {
                
                var customer = Mapper.FromModel(custModel);
                _customerRepository.Insert(customer);
                var users = _customerRepository.Login(customer.UserName, customer.Password);
                if (users.Count != 0)
                {
                    // ViewData["Messeage"] = "Thành công";
                    Session["custId"] = users[0].CustomerId;
                    ViewData["Messeage"] = Session["custId"];
                    Response.Write("<script>alert('Đăng kí tài khoản thành công');</script>");
                    return View();
                }
                else
                {
                    ViewData["Messeage"] = "Không thành công";
                    return View();
                }
            } 
            else
            {
                return View(custModel);
            }
            
        }
        //Customer login action
        public ActionResult Login()
        {
            return View();
        }
        //Customer login action form 
        [HttpPost]
        public ActionResult Login(CustomerLoginModel custLoginModel)
        {
            if (ModelState.IsValid)
            {
                CustomerModel custModel = new CustomerModel();
                custModel.UserName = custLoginModel.UserName;
                custModel.Password = custLoginModel.Password;
                var userLogin = Mapper.FromModel(custModel);
                var users = _customerRepository.Login(userLogin.UserName, userLogin.Password);
                if (users.Count != 0)
                {
                   
                    Session["custId"] = users[0].CustomerId;
                  
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewData["Messeage"] = "Không thành công";
                    return View();
                }
              
            }
            else
            {
                return View(custLoginModel);
            }
            
        }
        //Customer logout
        [HttpGet]
        public ActionResult Logout()
        {
            //Gan gia tri reset lai session 
            Session["custId"] = null;
            //Tro ve trang chu
            return RedirectToAction("../../Home");
        }

    }
}
